/**
 *@filename:t_i2c_m.h
 *@brief : I2C gpio driver file
 *+pach  const  function : t_i2c_create.
 */
#ifndef _T_I2C_M_H
#define _T_I2C_M_H


#include "stdint.h"
#include "stdio.h"
#include <stdlib.h>  
#ifdef __cplusplus
extern "C" {
#endif
#define T_NULL    NULL
#define T_FREE(p)   free(p)
#define T_MALLOC(p) malloc(p)

#define i2c_delay_time 120 // I2C software delay time

/* 错误码定义 */
typedef enum {
    I2C_OK = 0,
    I2C_ERR_NULL_PTR,
    I2C_ERR_NO_ACK,
    I2C_ERR_TIMEOUT,
    I2C_ERR_INIT_FAIL
} i2c_err_t;

typedef enum
{
	T_I2C_PIN_RESET = 0,
	T_I2C_PIN_SET = 1
} pin_value_e;
typedef void (*Ti2c_pincfg_func)(void *port, uint16_t pin);
typedef void (*Ti2c_delay_func)(uint32_t us);
typedef void (*Ti2c_write_func)(void *port, uint16_t pin, uint8_t pin_val);
typedef uint8_t (*Ti2c_read_func)(void *port, uint16_t pin);
typedef enum
{
	T_I2C_Block1 = 0,
	T_I2C_Block2 = 2
} T_I2C_Block;

typedef struct
{
	uint16_t pin_scl;
	uint16_t pin_sda;
	void *scl_port;
	void *sda_port;
} t_i2c_gpio_t;

typedef struct
{
	uint8_t dev_addr;// dev addr
	Ti2c_pincfg_func hwcfgOut_func; // cfg out
	Ti2c_pincfg_func hwcfgIn_func;
	Ti2c_delay_func delay_func; // cfg delay
	Ti2c_write_func write_func;
	Ti2c_read_func read_func;
	void *arg;
	const t_i2c_gpio_t *hw_gpio;
} t_i2c_t;

t_i2c_t *t_i2c_create(void *arg,
					  Ti2c_pincfg_func cfgout_func,
					  Ti2c_pincfg_func cfgin_func,
					  Ti2c_delay_func delay_func,
					  Ti2c_write_func i2c_w_func,
					  Ti2c_read_func i2c_r_func,
					  const t_i2c_gpio_t *hw_gpio);

void t_i2c_setDev_addr(t_i2c_t *me,uint8_t dev_addr);

void t_i2c_destroy(t_i2c_t *me);
void t_i2c_start(t_i2c_t *me);
void t_i2c_stop(t_i2c_t *me);
uint8_t t_i2c_wait_ack(t_i2c_t *me);

void t_i2c_send_byte(t_i2c_t *me, uint8_t byte);
uint8_t t_i2c_read_byte(t_i2c_t *me, uint8_t ack);

uint8_t t_i2c_write(t_i2c_t *me, uint8_t reg_addr, uint8_t data);
uint8_t t_i2c_read(t_i2c_t *me, uint8_t reg_addr, uint8_t *revdata);

int t_i2c_write_gp8312(t_i2c_t *me, uint16_t val);
uint8_t t_i2c_CheckDevice(t_i2c_t *me, uint8_t _Address);


uint8_t ip18_i2c_read(t_i2c_t *me, uint8_t pagaddr, uint8_t regaddr, uint16_t *regval_t);
uint8_t ip18_i2c_write(t_i2c_t *me, uint8_t pagaddr, uint8_t regaddr, uint16_t regval);

#ifdef __cplusplus
}
#endif

#endif
